package org.xiaov.lunasql.spring.proxy;

import org.xiaov.lunasql.engine.transform.TransformManager;
import org.xiaov.lunasql.spring.mybatis.DatabaseTypeHolder;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * The type Statement proxy.
 *
 * @author xiaovcloud
 * @since 2024/1/23 10:50
 */
public class LunaStatementProxy<T extends Statement> extends AbstractStatementProxy<T> {

    /**
     * Instantiates a new Statement proxy.
     *
     * @param connectionWrapper the connection wrapper
     * @param targetStatement   the target statement
     * @param targetSql         the target sql
     */
    public LunaStatementProxy(AbstractConnectionProxy connectionWrapper, T targetStatement, String targetSql) {
        super(connectionWrapper, targetStatement, targetSql);
    }

    /**
     * Instantiates a new Statement proxy.
     *
     * @param connectionWrapper the connection wrapper
     * @param targetStatement   the target statement
     */
    public LunaStatementProxy(AbstractConnectionProxy connectionWrapper, T targetStatement) {
        this(connectionWrapper, targetStatement, null);
    }

    /**
     * 获取proxy
     *
     * @return LunaConnectionProxy
     */
    @Override
    public LunaConnectionProxy getConnectionProxy() {
        return (LunaConnectionProxy) super.getConnectionProxy();
    }

    @Override
    public ResultSet executeQuery(String sql) throws SQLException {
        sql = transformSQL(sql);
        this.targetSql = sql;
        return targetStatement.executeQuery(sql);
    }

    @Override
    public int executeUpdate(String sql) throws SQLException {
        sql = transformSQL(sql);
        this.targetSql = sql;
        return targetStatement.executeUpdate(sql);
    }

    @Override
    public boolean execute(String sql) throws SQLException {
        sql = transformSQL(sql);
        this.targetSql = sql;
        return targetStatement.execute(sql);
    }


    @Override
    public int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException {
        sql = transformSQL(sql);
        this.targetSql = sql;
        return targetStatement.executeUpdate(sql, autoGeneratedKeys);
    }

    @Override
    public int executeUpdate(String sql, int[] columnIndexes) throws SQLException {
        sql = transformSQL(sql);
        this.targetSql = sql;
        return targetStatement.executeUpdate(sql, columnIndexes);
    }

    @Override
    public int executeUpdate(String sql, String[] columnNames) throws SQLException {
        sql = transformSQL(sql);
        this.targetSql = sql;
        return targetStatement.executeUpdate(sql, columnNames);
    }

    @Override
    public boolean execute(String sql, int autoGeneratedKeys) throws SQLException {
        sql = transformSQL(sql);
        this.targetSql = sql;
        return targetStatement.execute(sql, autoGeneratedKeys);
    }

    @Override
    public boolean execute(String sql, int[] columnIndexes) throws SQLException {
        sql = transformSQL(sql);
        this.targetSql = sql;
        return targetStatement.execute(sql, columnIndexes);
    }

    @Override
    public boolean execute(String sql, String[] columnNames) throws SQLException {
        sql = transformSQL(sql);
        this.targetSql = sql;
        return targetStatement.execute(sql, columnNames);
    }

    /**
     * 转换sql
     *
     * @param sql 需要转换的sql
     * @return 转换结果
     */
    protected String transformSQL(String sql) {
        return TransformManager.INSTANCE.parseSQL(sql, DatabaseTypeHolder.getDbType());
    }
}
